package com.dkm.biz.ou.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dkm.biz.ou.persistence.entity.ShopPoEnum;
import com.dkm.biz.ou.persistence.entity.ShopStatPo;
import com.dkm.biz.ou.persistence.entity.ShopStatPoEnum;
import com.dkm.biz.ou.repository.ShopStatRepository;
import com.dkm.biz.ou.service.ShopStatStatService;
@Service
public class ShopStatStatServiceImpl implements ShopStatStatService {
	@Resource
	private ShopStatRepository shopStatRepository;

	@Override
	public void addShopStat(String shopId, ShopStatPoEnum type, Integer totalSaleQyt) {
		ShopStatPo shopStatPo = shopStatRepository.findByShopIdAndType(shopId);
		if(shopStatPo == null){
			shopStatPo = new ShopStatPo();
			shopStatPo.setShopId(shopId);
			shopStatPo.setProductQty(0);
			shopStatPo.setDynamicQty(0);
			shopStatPo.setFollowQty(0);
			shopStatPo.setProductQty(0);
			shopStatPo.setStockQty(0);
		}
		if(ShopStatPoEnum.dynamic_qty_.equals(type)){
		    if(shopStatPo.getDynamicQty() == null){
		    	shopStatPo.setDynamicQty(0);
		    }
		    shopStatPo.setDynamicQty(shopStatPo.getDynamicQty()+totalSaleQyt);
		}
		if(ShopStatPoEnum.follow_qty_.equals(type)){
		    if(shopStatPo.getFollowQty() == null){
		    	shopStatPo.setFollowQty(0);
		    }
		    shopStatPo.setFollowQty(shopStatPo.getFollowQty()+totalSaleQyt);
		}
		if(ShopStatPoEnum.stock_qty_.equals(type)){
		    if(shopStatPo.getStockQty() == null){
		    	shopStatPo.setStockQty(0);
		    }
		    shopStatPo.setStockQty(shopStatPo.getStockQty()+totalSaleQyt);
		}
		if(ShopStatPoEnum.product_qty_.equals(type)){
		    if(shopStatPo.getProductQty() == null){
		    	shopStatPo.setProductQty(0);
		    }
		    shopStatPo.setProductQty(shopStatPo.getProductQty()+totalSaleQyt);
		}
		if(ShopStatPoEnum.sale_qty_.equals(type)){
		    if(shopStatPo.getSaleQty() == null){
		    	shopStatPo.setSaleQty(0);
		    }
		    shopStatPo.setSaleQty(shopStatPo.getSaleQty()+totalSaleQyt);
		}
		shopStatRepository.newInstance(shopStatPo).save();
	}

}
